package com.pxst.et.controller;

import com.github.pagehelper.Page;
import com.pxst.et.entity.NonMerchantStatementExcelModel;
import com.pxst.et.req.NonMerchantStatementReq;
import com.pxst.et.resp.NonMerchantStatementResp;
import com.pxst.et.service.NonMerchantStatementService;
import com.pxst.exception.BusinessException;
import com.pxst.resp.ApiResp;
import com.pxst.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author rj
 * @create 2023-11-03-9:59
 */
@RestController
@RequestMapping("/oto")
@Api(tags = "非商户对账单相关接口")
@Validated
public class NonMerchantStatementController {

    @Autowired
    private NonMerchantStatementService nonMerchantStatementService;

    @GetMapping
    @ApiOperation("查询非商户对账单数据")
    public ApiResp<Page<NonMerchantStatementResp>> getNonMerchantStatement(NonMerchantStatementReq nonMerchantStatementReq) {
        return nonMerchantStatementService.getPage(nonMerchantStatementReq);
    }

    @GetMapping("/download")
    @ApiOperation("汇出报表")
    public void download(NonMerchantStatementReq nonMerchantStatementReq, HttpServletResponse response){
        List<NonMerchantStatementExcelModel> nonMerchantStatementDownloads = nonMerchantStatementService.download(nonMerchantStatementReq);
        try {
            ExcelUtils.exportExcel(nonMerchantStatementDownloads, "非商户对账单汇出报表", "非商户对账单报表", NonMerchantStatementExcelModel.class, "OTO-", response);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("汇出报表错误");
        }
    }
}
